# 1.导入包
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# 2.加载环境变量
# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings.dev")

# 3.创建celery对象(work,看关系图)
# app = Celery('自定义名')
app = Celery('meiduo')

# 4.加载配置文件,(任务队列,结果队列)
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('celery_tasks.config')  # 'django.conf:settings', namespace='CELERY')

# 5.注册任务(任务方法一)
# 括号里面放列表,可承载多个任务
# app.autodiscover_tasks()
app.autodiscover_tasks(['celery_tasks.test.tasks', 'celery_tasks.sms.tasks'])


# 6.装饰任务(任务方法二),debug_task是具体任务,自定义名
# @app.task(bind=True)
# def hello_world(self):
#     # print('Request: {0!r}'.format(self.request))
#     import time
#     for i in range(0, 10):
#         time.sleep(1)
#         print('l = %s' % i)

# 任务使用方法:
# 在终端发送任务之前先导包
# from celery_tasks.test.tasks import hello_world
# 发送任务启动
# hello_world.delay()
# 不同的包处理不同的任务,可建立多个包
